<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Python 面试必须要看的16个问题, 湮灭星空,博客,Python,后端">
    <meta name="description" content="一、Python 是如何进行内存管理的？
答：从三个方面来说，对象的引用计数机制、垃圾回收机制、内存池机制



对象的引用计数机制
Python 内部使用引用计数，来保持追踪内存中的对象，所有对象都有引用计数。
引用计数增加的情况：

一">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Python 面试必须要看的16个问题 | 湮灭星空</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<link rel="alternate" href="/atom.xml" title="湮灭星空" type="application/atom+xml">
</head>

<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper head-container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">湮灭星空</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">湮灭星空</div>
        <div class="logo-desc">
            
            天下寥寥，苍生涂涂，诸子百家，唯我纵横
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友链
		</a>
          
        </li>
        
        
    </ul>
</div>

        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/18.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        Python 面试必须要看的16个问题
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/python/">
                                <span class="chip bg-color">python</span>
                            </a>
                        
                            <a href="/tags/interview/">
                                <span class="chip bg-color">interview</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Tech/" class="post-category">
                                Tech
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-04-15
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2019-05-14
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    2.1k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    8 分
                </div>
                
				
                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
            
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="一-python-是如何进行内存管理的？"><a class="header-anchor" href="#一-python-是如何进行内存管理的？"></a>一、Python 是如何进行内存管理的？</h2>
<p>答：从三个方面来说，对象的引用计数机制、垃圾回收机制、内存池机制</p>
<a id="more"></a>
<ol>
<li>
<p>对象的引用计数机制<br>
Python 内部使用引用计数，来保持追踪内存中的对象，所有对象都有引用计数。<br>
引用计数增加的情况：</p>
<ol>
<li>一个对象分配一个新名称；</li>
<li>将其放入一个容器中（列表、元组或字典）</li>
</ol>
<p>引用计数减少的情况：</p>
<ol>
<li>使用 <code>del</code>语句对对象别名显示的销毁</li>
<li>引用超出作用域或被重新赋值</li>
</ol>
<p><code>sys.getrefcount()</code>函数可以获得当前对象的当前引用计数多数情况下，引用计数比你猜测的要大得多。对于不可变数据（数字、字符串、元组），解释器会在程序的不同部分共享内存，一遍节约内存。</p>
</li>
<li>
<p>垃圾回收</p>
<ol>
<li>当一个对象的引用计数归零时，它将被垃圾收集机制处理掉；</li>
<li>当两个对象 a 和 b 相互引用时，<code>del</code> 语句可以减少 a 和 b 的引用计数，并销毁用于引用底层对象的名称。</li>
</ol>
<p>然而由于每个对象都包含一个对其他对象的应用，因此引用计数不会归零，对象也不会销毁。从而导致内存泄漏，为解决这一问题，解释器会定期执行一个循环检测器，搜索不可访问对象的循环并删除他们。</p>
</li>
<li>
<p>内存池机制<br>
Python 提供了对内存的垃圾收集机制，但是它将不用的内存放到内存池而不是返回给操作系统。</p>
<ol>
<li><code>Pymalloc</code>机制：为了加速 Python 的执行效率，Python 引入了一个内存池机制，用于管理对小块内存的申请和释放；</li>
<li>Python 中所有小于256字节的对象都使用<code>Pymalloc</code>实现的分配器，而大的对象则使用系统的 <code>malloc</code>；</li>
<li>对于 Python 对象，如整数、浮点数和 List，都有其独立的私有内存池，对象间不共享他们的内存池。也就是说如果你分配又释放了大量的整数，用于缓存这些整数的内存就不能再分配给浮点数了。</li>
</ol>
</li>
</ol>
<h2 id="二-什么是-lambda-函数？它的好处是什么？"><a class="header-anchor" href="#二-什么是-lambda-函数？它的好处是什么？"></a>二、什么是 lambda 函数？它的好处是什么？</h2>
<p>答：lambda 表达式，通常是在需要一个函数，但是又不想费神去命名一个函数的场合下使用，也就是指匿名函数。</p>
<p>优点：是一个短小的回调函数</p>
<pre><code class="language-python">lambda [args]: expression 
lambda x, y: x + y
</code></pre>
<h2 id="三-python-里面如何实现-tuple-和-list-的转换？"><a class="header-anchor" href="#三-python-里面如何实现-tuple-和-list-的转换？"></a>三、Python 里面如何实现 tuple 和 list 的转换？</h2>
<p>答：使用内置的类型构造方法<code>list()</code>、<code>tuple()</code>，<code>type()</code>可以判断对象的类型</p>
<h2 id="四-请写出一段-python-代码实现删除一个-list-里面的重复元素"><a class="header-anchor" href="#四-请写出一段-python-代码实现删除一个-list-里面的重复元素"></a>四、请写出一段 Python 代码实现删除一个 list 里面的重复元素</h2>
<p>答：</p>
<ol>
<li>
<p>使用 <code>set</code> 函数，<code>set(list)</code></p>
<pre><code class="language-python">my_list = [1, 1, 2, 2, 3, 3, ]
my_list = list(set(my_list))
print(my_list)
-----------------------------------
[1, 2, 3]
</code></pre>
</li>
<li>
<p>使用字典函数</p>
<pre><code class="language-python">a = [1, 2, 4, 2, 4, 5, 6, 5, 7, 8, 9, 0]
b = {}
b = b.fromkeys(a)
c = list(b.keys())
print('a: ', a)
print('b: ', b)
print('c: ', c)
----------------------------------------
a:  [1, 2, 4, 2, 4, 5, 6, 5, 7, 8, 9, 0]
b:  {1: None, 2: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None, 0: None}
c:  [1, 2, 4, 5, 6, 7, 8, 9, 0]
</code></pre>
</li>
</ol>
<h2 id="五-用-sort-排序-然后从最后一个元素开始判断"><a class="header-anchor" href="#五-用-sort-排序-然后从最后一个元素开始判断"></a>五、用 sort 排序，然后从最后一个元素开始判断</h2>
<p>思路：</p>
<ol>
<li>使用 range 将列表的索引倒序排序</li>
<li>last 与前一个元素对比
<ul>
<li>重复：删除前一个一个元素</li>
<li>不重复：将前一个元素赋值给 last</li>
</ul>
</li>
</ol>
<pre><code class="language-python">a = [1, 2, 4, 2, 4, 5, 7, 10, 5, 5, 7, 8, 9, 0, 3]
a.sort()
last = a[-1]
for i in range(len(a)-2, -1, -1):
    if last == a[i]:
        print(a[i], 'repeat')
        a.remove(a[i])
    else:
        last = a[i]
print(a)
------------------------------------------------
7 repeat
5 repeat
5 repeat
4 repeat
2 repeat
[0, 1, 2, 3, 4, 5, 7, 8, 9, 10]
</code></pre>
<h2 id="六-python-如何拷贝一个对象-赋值-浅拷贝-深拷贝的区别"><a class="header-anchor" href="#六-python-如何拷贝一个对象-赋值-浅拷贝-深拷贝的区别"></a>六、Python 如何拷贝一个对象（赋值、浅拷贝、深拷贝的区别）</h2>
<p>答：</p>
<ol>
<li>赋值：就是创建了对象的新引用，修改其中任意一个变量都会影响到另一个对象。</li>
<li>浅拷贝：创建一个新的对象，但它包含的是对原始对象中包含项的引用<br>
（如果用引用的方式修改其中一个对象，另一个对象也会改变）<br>
浅拷贝的方式：
<ul>
<li>完全切片方法</li>
<li>工厂函数：<code>list()</code>等</li>
<li><code>copy</code> 模块的 <code>copy()</code>函数</li>
</ul>
</li>
<li>深拷贝：创建一个新对象，并且递归地复制它所包含的对象<br>
（修改其中一个，另一个不会改变）<br>
深拷贝的方式：
<ul>
<li><code>copy</code>模块的<code>deepcopy()</code>函数</li>
</ul>
</li>
</ol>
<h2 id="七-介绍一下-except-的用法和作用"><a class="header-anchor" href="#七-介绍一下-except-的用法和作用"></a>七、介绍一下 except 的用法和作用</h2>
<pre><code class="language-python">try:
    main_action
except Exception1:
    handler1
except Exception2:
    handler2
...
else:
    else_block
finally:
    finally_block
</code></pre>
<p>执行 main_action，如果引发异常，则执行过程会跳到 except 语句。对每个 except 分支顺序尝试执行，如果引发的异常与 except 中的异常匹配，则执行相应的语句；</p>
<p>如果所有 except 都不匹配，则异常会传递到下一个调用本代码的最高层 try 代码中。</p>
<p>如果没有执行 main_action 没有引发异常，则执行 else_block；</p>
<p>最后无论之前执行了什么、有没有引发异常、异常有没有处理，都会执行 finally_block。</p>
<p>finally 并不终止异常，如果异常没有被处理，还会继续往后传递。</p>
<h2 id="八-python-中的-pass-语句的作用是什么？"><a class="header-anchor" href="#八-python-中的-pass-语句的作用是什么？"></a>八、Python 中的 pass 语句的作用是什么？</h2>
<p>答：pass 不会执行任何操作，一般作为占位符或者创建占位程序</p>
<h2 id="九-介绍一下-python-下的-range-函数的用法？"><a class="header-anchor" href="#九-介绍一下-python-下的-range-函数的用法？"></a>九、介绍一下 Python 下的 range() 函数的用法？</h2>
<pre><code class="language-python">range(start, stop, step)
start:	起始位置
stop :	结束位置(不包含末位)
step :	步进
    
a = [1, 2, 3]
for i in range(len(a)):
    print(a[i])
---------------------------
1
2
3
</code></pre>
<h2 id="十-如何使用-python-查询和替换一个文本字符串？"><a class="header-anchor" href="#十-如何使用-python-查询和替换一个文本字符串？"></a>十、如何使用 Python 查询和替换一个文本字符串？</h2>
<p>答：</p>
<ol>
<li>
<p>可以使用 <code>re</code>模块中的<code>sub()</code>函数或者<code>subn()</code>函数来进行查询和替换。</p>
<pre><code class="language-python">re.sub(pattern, repl, string, count=0, flags=0)
</code></pre>
<p>参数：</p>
<ul>
<li><code>pattern</code> : 正则中的模式字符串。</li>
<li><code>repl</code> : 替换的字符串，也可为一个函数。</li>
<li><code>string</code> : 要被查找替换的原始字符串。</li>
<li><code>count</code> : 模式匹配后替换的最大次数，默认 0 表示替换所有的匹配。</li>
</ul>
<p><code>subn()</code>方法执行的效果跟<code>sub()</code>一样，不过它会返回一个二维数组，包括替换后的新的字符串和总共替换的数量。</p>
</li>
<li>
<p>字符串内置<code>replace()</code>方法</p>
<pre><code class="language-python">replace(old, new, count)
</code></pre>
</li>
</ol>
<h2 id="十一-python-里面match-和search-的区别？"><a class="header-anchor" href="#十一-python-里面match-和search-的区别？"></a>十一、Python 里面<code>match()</code>和<code>search()</code>的区别？</h2>
<p><code>re.match</code> 尝试从字符串的<strong>起始位置匹</strong>配一个模式，如果不是起始位置匹配成功的话，match()就返回none。</p>
<p><code>re.search</code> 扫描整个字符串并返回<strong>第一个成功的匹配</strong>。</p>
<pre><code class="language-python">&gt;&gt;&gt;print(re.match(‘super’, ‘superstition’).span())

(0, 5)

&gt;&gt;&gt;print(re.match(‘super’, ‘insuperable’))

None

&gt;&gt;&gt;print(re.search(‘super’, ‘superstition’).span())

(0, 5)

&gt;&gt;&gt;print(re.search(‘super’, ‘insuperable’).span())

(2, 7)
</code></pre>
<h2 id="十二-用-python-匹配-html-tag-的时候-和-有什么区别？"><a class="header-anchor" href="#十二-用-python-匹配-html-tag-的时候-和-有什么区别？"></a>十二、用 Python 匹配 HTML tag 的时候，&lt;.*&gt; 和 &lt;.*?&gt; 有什么区别？</h2>
<p>答：贪婪模式和非贪婪模式的区别。不加 ? 的为默认贪婪模式，加 ？的为非贪婪模式。</p>
<pre><code class="language-python">import re
my_str = '&lt;div class&gt;' \
    '&lt;div class&gt;' \
    '&lt;h2&gt;What?&lt;/h2&gt;' \
    '&lt;p&gt;The&lt;/p&gt;'
result1 = re.findall('&lt;.*&gt;', my_str)
result2 = re.findall('&lt;.*?&gt;', my_str)
print(result1)
print(result2)
-------------------------------------
['&lt;div class&gt;&lt;div class&gt;&lt;h2&gt;What?&lt;/h2&gt;&lt;p&gt;The&lt;/p&gt;']
['&lt;div class&gt;', '&lt;div class&gt;', '&lt;h2&gt;', '&lt;/h2&gt;', '&lt;p&gt;', '&lt;/p&gt;']
</code></pre>
<h2 id="十三-python-里面如何生成随机数？"><a class="header-anchor" href="#十三-python-里面如何生成随机数？"></a>十三、Python 里面如何生成随机数？</h2>
<p>答：<code>random</code>模块</p>
<p>随机整数：</p>
<ul>
<li><code>x = random.randint(a, b)</code>：<code>a &lt; x &lt; b</code></li>
<li><code>x = random.randrange(a, b, c)</code>
<ul>
<li>a：start</li>
<li>b：stop</li>
<li>c：step</li>
</ul>
</li>
</ul>
<p>随机实数：</p>
<ul>
<li><code>x = random.random()</code>：x 为随机实数</li>
<li><code>x = random.uniform(a, b)</code>：<code>a &lt; x &lt; b</code>
<ul>
<li>a：start</li>
<li>b：stop</li>
</ul>
</li>
</ul>
<pre><code class="language-python">import random
a = random.randint(0, 10)
b = random.randrange(0, 10, 2)
c = random.random()
d = random.uniform(0, 10)
print(a)
print(b)
print(c)
print(d)
--------------------------------------
2
8
0.9532895768014032
4.443647403291584
</code></pre>
<h2 id="十四-有没有一个工具可以帮忙查找-python-的-bug-和进行静态代码分析？"><a class="header-anchor" href="#十四-有没有一个工具可以帮忙查找-python-的-bug-和进行静态代码分析？"></a>十四、有没有一个工具可以帮忙查找 python 的 bug 和进行静态代码分析？</h2>
<p>答：<code>PyChecker</code> 是一个 python 代码的静态分析工具，它可以帮助查找 python 代码的 bug，会对代码的复杂度和格式提出警告。</p>
<p><code>Pylint</code> 是另一个工具可以进行 codingstandard 检查。</p>
<h2 id="十五-如何在一个-function-里设置一个全局变量？"><a class="header-anchor" href="#十五-如何在一个-function-里设置一个全局变量？"></a>十五、如何在一个 function 里设置一个全局变量？</h2>
<p>答：使用<code>global</code>关键字</p>
<h3 id="十六-单引号-双引号-三引号的区别？"><a class="header-anchor" href="#十六-单引号-双引号-三引号的区别？"></a>十六、单引号、双引号、三引号的区别？</h3>
<p>答：单引号和双引号是等效的，如果要换行，则需要<code>\</code>转义，三引号直接换行，并且可以包含注释</p>
<p>如果要表示<code>Let's go</code>这个字符串：</p>
<p>单引号：<code>s4 = 'Let's go'</code></p>
<p>双引号：<code>s4 = &quot;Let's go&quot;</code></p>
<p><code>s6 = 'I realy like &quot;python&quot;!'</code></p>

<div id="gitalk-container"></div>
<script src="https://cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script><link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css"><script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>

		<script>
		var gitalkConfig = {"clientID":"9eb5bc3ac1e1ff3ddac0","clientSecret":"4b7ae28042282281295075c2bf0c97ff1791cfeb","repo":"HexoBlogComments","owner":"Annihilater","admin":["Annihilater"],"distractionFreeMode":false};
	    gitalkConfig.id = md5(location.pathname);
		var gitalk = new Gitalk(gitalkConfig);
	    gitalk.render("gitalk-container");
	    </script>
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://www.klause.cn" rel="external nofollow noreferrer">湮灭星空</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://www.klause.cn/2019/04/15/mian-shi/python-mian-shi-bi-xu-yao-kan-de-16-ge-wen-ti/">https://www.klause.cn/2019/04/15/mian-shi/python-mian-shi-bi-xu-yao-kan-de-16-ge-wen-ti/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://www.klause.cn" target="_blank">湮灭星空</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/python/">
                                    <span class="chip bg-color">python</span>
                                </a>
                            
                                <a href="/tags/interview/">
                                    <span class="chip bg-color">interview</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/04/15/mian-shi/python-yan-fa-gong-cheng-shi-shi-ti/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/4.jpg" class="responsive-img" alt="python 研发工程师试题">
                        
                        <span class="card-title">python 研发工程师试题</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            python 研发工程师试题

python 中list、tuple、dict、set有什么区别，主要应用在什么样的场景？并用 for语句分别进行遍历。


答：


list：列表[]


应用场景：


for：
# 循环列表
list
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2019-04-15
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Tech/" class="post-category">
                                    Tech
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                    <a href="/tags/interview/">
                        <span class="chip bg-color">interview</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/04/15/mian-shi/mian-shi-wen-ti/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/5.jpg" class="responsive-img" alt="面试问题">
                        
                        <span class="card-title">面试问题</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
python3的类是深度遍历还是广度遍历？
写个快排
tcp的 三次握手和四次挥手






		
		var gitalkConfig = {"clientID":"9eb5bc3ac1e1ff3ddac0","clientSecre
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2019-04-15
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Tech/" class="post-category">
                                    Tech
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                    <a href="/tags/interview/">
                        <span class="chip bg-color">interview</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>

    
<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>

    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">年份</span>
            <a href="https://www.klause.cn" target="_blank">湮灭星空</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">199.2k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    window.setTimeout("siteTime()", 1000);
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2019";
                    var startMonth = "6";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
            <span id="icp"><img src="/medias/icp.png" style="vertical-align: text-bottom;" />
                <a href="http://www.beian.miit.gov.cn" target="_blank">皖ICP备18005729号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/Annihilater" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:yanmiexingkong@gmail.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>





    <a href="https://twitter.com/" class="tooltipped" target="_blank" data-tooltip="关注我的Twitter: https://twitter.com/" data-position="top" data-delay="50">
        <i class="fab fa-twitter"></i>
    </a>







    <a href="https://www.zhihu.com/" class="tooltipped" target="_blank" data-tooltip="关注我的知乎: https://www.zhihu.com/" data-position="top" data-delay="50">
        <i class="fab fa-zhihu1">知</i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>

    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    <script type="text/javascript" src="/libs/background/ribbon-dynamic.js" async="async"></script>
    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
